﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using log4net;
using ForumLibrary;
using ForumLibrary.Data;

namespace ForumGUI
{
    public partial class RegisterFrame : Form//, IObserver<IForumManager>
    {
        private ForumManager _forumManager;
        private string _username1;
        private string _password1;
        private string _confirmedPassword1;
        private string _email1;
        private string _phoneNumber1;
        private string _firstName1;
        private string _lastName1;

        public RegisterFrame(ref ForumManager forumManager)
        {
            InitializeComponent();
            this._forumManager = forumManager;
            _username1 = "";
            _password1 = "";
            _email1 = "";
            _phoneNumber1 = "";
            _firstName1 = "";
            _lastName1 = "";
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            this._username1 = username.Text;
            this._password1 = password.Text;
            this._confirmedPassword1 = confirmPassword.Text;
            this._email1 = email.Text;
            this._phoneNumber1 = phoneNumber.Text;
            this._firstName1 = firstName.Text;
            this._lastName1 = lastName.Text;
            if (string.IsNullOrEmpty(this._username1))
            {
                MessageBox.Show("Please enter a username!!!");
                return ;
            }
            if (string.IsNullOrEmpty(this._password1))
            {
                MessageBox.Show("Please enter a password!!!");
                return;
            }
            if ((string.IsNullOrEmpty(this._confirmedPassword1)) || (!this._password1.Equals(this._confirmedPassword1))) 
            {
                MessageBox.Show("please confirm password again!!!");
                return;
            }
            if (string.IsNullOrEmpty(this._email1))
            {
                MessageBox.Show("Please enter an email!!!");
                return;
            }
            if (string.IsNullOrEmpty(this._firstName1))
            {
                MessageBox.Show("Please enter a first name!!!");
                return;
            }
            if (string.IsNullOrEmpty(this._lastName1))
            {
                MessageBox.Show("Please enter a last name!!!");
                return;
            }
            
            bool moderator = false;
            if (moderatorCheckBox.Checked)
            {
                try
                {
                    moderator = _forumManager.checkModeratorPassword(moderatorTextBox.Text);
                }
                catch
                {
                    MessageBox.Show("Server is down. Please try again in a few minutes");
                }
                if (moderator == false)
                {
                    MessageBox.Show("Incorrect Moderator password!!!");
                    return;
                }
            }
            string res = "";
            try
            {
                //register(string firstName, string lastName, string username, string password, string email, string phone)
                res = this._forumManager.register(_firstName1, _lastName1,_username1, ForumManager.Encoder.HashEncode(_password1).ToLower(), _email1, _phoneNumber1, moderator);
            }
            catch (Exception)
            {
                MessageBox.Show("Server is down. Please try again in a few minutes");
            } 
            if (res.Contains("Username is not available"))
            {
                MessageBox.Show("Username is not available");
            }
            else if (res.Contains("Registration Complete"))
            {
                MessageBox.Show("Registration Complete!");
                this.Dispose(false);
                new LoginFrame(ref _forumManager).Visible = true;
            }
           
        }
        /*
        private void username_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._username1 = t.Text;
        }

        private void password_TextChanged(object sender, EventArgs e)
        {
            //this._password1 = ForumServer.hashEncode.HashEncode(sender.ToString());
            //TODO: encrypt the input using benny's class.could not acsses it for some reason.
            TextBox t = (TextBox)sender;
            this._password1 = t.Text;
        }
        private void confirmPassword_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._confirmedPassword1 = t.Text;//TODO:change this to encrypt mode/

        }

        private void email_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._email1 = t.Text;
        }

        private void phoneNumber_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._phoneNumber1 = t.Text;
        }

        private void firstName_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._firstName1 = t.Text;
        }

        private void lastName_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            this._lastName1 = t.Text;
        }

        public void OnCompleted()
        {
            throw new NotImplementedException();
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnNext(IForumManager value)
        {
            throw new NotImplementedException();
        }

        private void _password_Click(object sender, EventArgs e)
        {

        }

        private void PasswordConfirm_Click(object sender, EventArgs e)
        {

        }
        */
        private void backButton_Click(object sender, EventArgs e)
        {
            LoginFrame l = new LoginFrame(ref _forumManager);
            l.Visible = true;
            //LoginFrame l = new LoginFrame(ref this._forumManager).Visible = true;
            this.Dispose(false);
        }

        private void moderatorCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            this.moderatorTextBox.Text = "";
            if (moderatorCheckBox.Checked)
                this.moderatorTextBox.Visible = true;
            else
                this.moderatorTextBox.Visible = false;

        }

        
      
    }
}
